from rest_framework.authentication import BaseAuthentication
from rest_framework.exceptions import AuthenticationFailed
from rest_framework import status
import re

class ClientIDAuthentication(BaseAuthentication):
   
    def authenticate(self, request):
        client_id = request.META.get('HTTP_CLIENT_ID')
        if not client_id:
            raise AuthenticationFailed({"status": status.HTTP_400_BAD_REQUEST, "errorCode": '10001', "errorMsg": "missing parameter"})
        
        if not re.match(r'^\d+$', client_id) or len(client_id) != 10:
            raise AuthenticationFailed({"status": status.HTTP_400_BAD_REQUEST, "errorCode": '10002', "errorMsg": "illegal parameter"})
        
        user = { 'pk': client_id }
        return user, client_id
    
    def authenticate_header(self, request):
        return 'client_id'